package com.snda.sc.utils;

import java.util.concurrent.atomic.AtomicLong;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 分业务日志记录。
 * 
 * @author <a href="mailto:seenoevil.cn@gmail.com">李曦</a><br/>
 *         Created at: 2012-9-27 下午6:18:46
 */
public class Logs {

	private static final AtomicLong batchVersion = new AtomicLong();
	private static final Logger sysInfoLog = LoggerFactory.getLogger("sysInfo");
	private static final Logger authInfoLog = LoggerFactory.getLogger("authInfo");
	private static final Logger checkcodeInfoLog = LoggerFactory.getLogger("checkcodeInfo");
	private static final Logger authFailureInfoLog = LoggerFactory.getLogger("authFailureInfo");
	private static final Logger exceptionLog = LoggerFactory.getLogger("exceptionInfo");
	private static final ThreadLocal<String> batch = new ThreadLocal<String>() {
		@Override
		protected String initialValue() {
			return "Sys";
		}
	};

	public static void updateVersion() {
		batchVersion.compareAndSet(Long.MAX_VALUE / 2, 0L);
		batch.set("Proc" + batchVersion.incrementAndGet());
	}

	/** 记录系统普通信息 */
	public static void logSysInfo(String msg, Object... params) {
		sysInfoLog.info("batch:" + batch.get() + ": " + msg, params);
	}

	/** 记录Auth认证中的流程信息 */
	public static void logAuthInfo(String msg, Object... params) {
		authInfoLog.info("batch:" + batch.get() + ": " + msg, params);
	}

	/** 记录CheckCode的信息 */
	public static void logCheckCodeInfo(String msg, Object... params) {
		checkcodeInfoLog.info("batch:" + batch.get() + ": " + msg, params);
	}

	/** 记录Auth失败信息 */
	public static void logFailure(String msg, Object... params) {
		authFailureInfoLog.info("batch:" + batch.get() + ": " + msg, params);
	}

	/** 记录异常信息 */
	public static void logException(String msg) {
		exceptionLog.error("batch:" + batch.get() + ": " + msg);
	}

	/** 记录异常信息 */
	public static void logException(String msg, Object... params) {
		exceptionLog.error("batch:" + batch.get() + ": " + msg, params);
	}

	/** 记录异常信息 */
	public static void logException(String msg, Throwable cause) {
		exceptionLog.error("batch:" + batch.get() + ": " + msg, cause);
	}
}
